package top.ahwyy.controller.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import top.ahwyy.controller.inter.card;
import top.ahwyy.controller.inter.user;
import top.ahwyy.model.carddata;
import top.ahwyy.model.userReplydata;
import top.ahwyy.model.userdata;
import top.ahwyy.model.zanUserdata;
import top.ahwyy.utils.databaseConnection;
import top.ahwyy.utils.dateTime;
import top.ahwyy.utils.filterSpecialChar;

public class cardImpl implements card {


	@Override
	public String getJsonCardsList(int pages, int showTotal) {
		// TODO Auto-generated method stub
		
		Connection conn = databaseConnection.getADBConnection();
		String sql = "SELECT userdata.userLogo AS lg,userdata.userName as un,carddata.title as tt,carddata.forumTypeName as ty,carddata.forumId as fid,carddata.userId as uid,carddata.content as ct FROM carddata,userdata "+
		"WHERE carddata.userId=userdata.userId ORDER BY carddata.lastUpdate DESC LIMIT ?,?;";
		
//		arg0:作者头像 
//		arg1：作者姓名
//		arg2：帖子主题
//		arg3：帖子分类
//		arg4:cardMain(帖子id)
//		arg5:帖子内容
//		arg6：赞href
//		arg7：赞数
//		arg8：评论href
//		arg9：评论数
		
		String str = "[";
		
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, pages*showTotal);
			stmt.setInt(2, showTotal);
			
			
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				String lg = rs.getString("lg");
				String un = rs.getString("un");
				String tt = rs.getString("tt");
				String ty = rs.getString("ty");
				int fid = rs.getInt("fid");
				//int uid = rs.getInt("uid");
				String ct = rs.getString("ct");
				
				String arg0 = lg;
				if(arg0 == null)
					arg0 = "arg0";
				String arg1 = filterSpecialChar.filterSpecialChar2(un);
				String arg2 = filterSpecialChar.filterSpecialChar2(tt);
				String arg3 = ty;
				String arg4 = fid+"";
				String arg5 = filterSpecialChar.filterSpecialChar2(ct);
				if(arg5.length() >= 270)
					arg5 = arg5.substring(0,270)+"......&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;更多";
				
				
				
				
				String arg6 = arg4+" "+this.getUserIdByUn(un);//"userZan?forumId="+arg4+"&uid="+this.getUserIdByUn(un);
				String arg7 = "赞";
				String arg8 = "2";
				String arg9 = "评论";
				
				str = str+this.getJsonCardsList(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
				str = str+",";
				
			}
			
			str = str.substring(0,str.length()-1);
			str = str+"]";

			//System.out.print(str);
			rs.close();
			
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return str;
	}
	//将字符串中转义字符替换掉
	
	public String getJsonCardsList(String arg0, String arg1, String arg2, String arg3, String arg4, String arg5,
			String arg6, String arg7, String arg8, String arg9) {
		// TODO Auto-generated method stub
		String str = "\"arg0\":\""+arg0+
				"\",\"arg1\":\""+arg1+
				"\",\"arg2\":\""+arg2+
				"\",\"arg3\":\""+arg3+
				"\",\"arg4\":\""+arg4+
				"\",\"arg5\":\""+arg5+
				"\",\"arg6\":\""+arg6+
				"\",\"arg7\":\""+arg7+
				"\",\"arg8\":\""+arg8+
				"\",\"arg9\":\""+arg9+"\"";
		
		str = "{"+str+"}";
//		arg0:作者头像
//		arg1：作者姓名
//		arg2：帖子主题
//		arg3：帖子分类
//		arg4:cardMain(帖子id)
//		arg5:帖子内容
//		arg6：赞href
//		arg7：赞数
//		arg8：评论href
//		arg9：评论数
		return str;
	}
	//根据当前页数 和 要显示的数量 来返回多少数据
	
	//通过forumId 找到forum信息
	public carddata getCardMessage(int forumId){
		carddata da = null;
		return da;
	}
	
	//通过userId 找到user信息
	public userdata getCardUserMessage(int userId){
		userdata da = null;
		return da;
	}
	
	//通过帖子id 找到帖子的回复数 
	public int getCradReplyTotal(int forumId){
		return 0;
	}
	
	//根据帖子id 找到帖子的赞数
	public int getCardZanTotal(int forumId){
		return 0;
	}
	
	@Override
	public int getUserIdByUn(String un){
		int id = 0;
		Connection conn = databaseConnection.getADBConnection();
		String sql = "select userId from userdata where userName = ?";
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, un);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				id = rs.getInt("userId");
			}
			
			rs.close();
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return id;
	}
	
	@Override
	public boolean saveCardMessage(String title, String content, String forumTypeName, String un) {
		// TODO Auto-generated method stub
		boolean flag = false;
		int userId = this.getUserIdByUn(un);
		String sql = "insert into carddata (title,content,emitTime,lastUpdate,forumTypeName,userId) values(?,?,?,?,?,?)";

		Connection conn = databaseConnection.getADBConnection();
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setString(1, title);
			stmt.setString(2, content);
			stmt.setTimestamp(3, dateTime.getDateTime());
			stmt.setTimestamp(4, dateTime.getDateTime());
			stmt.setString(5, forumTypeName);
			stmt.setInt(6, userId);
			

			//System.out.println(" title "+title+" content "+content+" forumTypeName "+forumTypeName+" userId "+userId+" un "+un);
			
			stmt.executeUpdate();
			flag = true;
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return flag;
	}

	@Override
	public String getContentByForumId(int fid) {
		// TODO Auto-generated method stub
		String ct = "false";
		Connection conn = databaseConnection.getADBConnection();
		String sql = "select content from carddata where forumId = "+fid;
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				ct = rs.getString("content");
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ct;
	}

	@Override
	public String getLastUpdateTimeByForumId(int fid) {
		// TODO Auto-generated method stub
		String lt = "false";
		Connection conn = databaseConnection.getADBConnection();
		String sql = "select lastUpdate from carddata where forumId = "+fid;
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				lt = rs.getTimestamp("lastUpdate").toString();
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lt;
	}

	@Override
	public ArrayList<zanUserdata> getZanUserList(int forumId) {
		// TODO Auto-generated method stub
		ArrayList<zanUserdata> al = new ArrayList<zanUserdata>();
		Connection conn = databaseConnection.getADBConnection();
		user ui = new userImpl();
		
		String sql = "select * from cardzandata where forumId = "+forumId;
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				int uid = rs.getInt("userId");
				String ulogo = ui.getUserLogoByUserId(uid);
				String un = ui.getUserNameByUserId(uid);
				zanUserdata zu = new zanUserdata();
				zu.setUid(uid);
				zu.setUlogo(ulogo);
				zu.setUn(un);
				
				al.add(zu);
			}
			
			rs.close();
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return al;
	}

	@Override
	public boolean userZan(int forumId, int uid) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = databaseConnection.getADBConnection();
		String sql = "insert into cardzandata (forumId,userId) values(?,?);";

		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setInt(1, forumId);
			stmt.setInt(2, uid);
			stmt.executeUpdate();
			flag = true;
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

	@Override
	public boolean insertUserReply(String content, int forumId, int uid) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Connection conn = databaseConnection.getADBConnection();
		String sql = "insert into cardreplydata (replyTime,content,userId,forumId) values (?,?,?,?);";
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setTimestamp(1, dateTime.getDateTime());
			stmt.setString(2, content);
			stmt.setInt(3, uid);
			stmt.setInt(4, forumId);
			stmt.executeUpdate();
			flag = true;
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return flag;
	}

	@Override
	public ArrayList<userReplydata> getReplyList(int forumId) {
		// TODO Auto-generated method stub
		ArrayList<userReplydata> al = new ArrayList<userReplydata>();
		user ui = new userImpl();
		Connection conn = databaseConnection.getADBConnection();
		String sql = "select * from cardreplydata where forumId = "+forumId;
		
		System.out.print(sql);
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			int floorNumber = 1;
			while(rs.next()){
				userReplydata ur = new userReplydata();
				
				int uid = rs.getInt("userId");
				String un = ui.getUserNameByUserId(uid);
				String content = rs.getString("content");
				
				ur.setFloorNumber(floorNumber);
				ur.setContent(content);
				ur.setUid(uid);
				ur.setUn(un);
				
				al.add(ur);
				floorNumber +=1;
			}
			rs.close();
			stmt.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return al;
	}

	/* 根据关键字搜索帖子 
	 * 将帖子内容标题等与关键字进行对比
	 * 有的话添加进forumId数组
	 * */
	@Override
	public ArrayList<?> searchCardByKeyWord(String kw) {
		// TODO Auto-generated method stub
		ArrayList<Integer> li = new ArrayList<Integer>();
		String content = null;
		String sql = "select * from carddata";
		Connection conn = databaseConnection.getADBConnection();
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				content = null;
				content+=rs.getString("title")+rs.getString("content")+rs.getString("forumTypeName");
				if(content.indexOf(kw) > -1){
					li.add(rs.getInt("forumId"));
				}
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return li;
	}

	@Override
	public carddata getACarddataObjectByForumId(int forumId) {
		// TODO Auto-generated method stub
		carddata cd = null;
		String sql = "select * from carddata where forumId = "+forumId;
		Connection conn = databaseConnection.getADBConnection();
		try {
			PreparedStatement stmt = conn.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while(rs.next()){
				cd = new carddata();
				cd.setContent(rs.getString("content"));
				cd.setEmitTime(rs.getTimestamp("emitTime").toString());
				cd.setForumId(rs.getInt("forumId"));
				cd.setForumTypeName(rs.getString("forumTypeName"));
				cd.setLastUpdate(rs.getTimestamp("lastUpdate").toString());
				cd.setTitle(rs.getString("title"));
				cd.setUserId(rs.getInt("userId"));
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return cd;
	}

}
